package com.plpm.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.plpm.service.IoTDBService;

import lombok.extern.slf4j.Slf4j;

/**
 * IoTDB数据库操作控制器
 * 
 * @author PLPM Team
 * @version 1.0
 */
@Slf4j
@Controller
@RequestMapping("/iotdb")
public class IoTDBController {

    @Autowired
    private IoTDBService iotdbService;

    /**
     * 根据file_name查询record_id列表
     * 
     * @param fileName 文件名
     * @return record_id列表
     */
    @GetMapping("/query-record-ids-by-file-name")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> queryRecordIdsByFileName(@RequestParam("fileName") String fileName) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            log.info("收到查询请求 - fileName: {}", fileName);
            
            // 参数验证
            if (fileName == null || fileName.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "fileName参数不能为空");
                return ResponseEntity.badRequest().body(result);
            }
            
            // 调用Service层方法执行查询
            Map<String, Object> queryResult = iotdbService.queryRecordIdsByFileName(fileName);
            
            // 返回查询结果
            if ((Boolean) queryResult.get("success")) {
                result.put("success", true);
                result.put("message", queryResult.get("message"));
                result.put("recordIds", queryResult.get("recordIds"));
                result.put("count", queryResult.get("count"));
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", queryResult.get("message"));
                result.put("recordIds", queryResult.get("recordIds"));
                return ResponseEntity.badRequest().body(result);
            }
            
        } catch (Exception e) {
            log.error("查询IoTDB record_id列表时发生异常 - fileName: {}", fileName, e);
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
            result.put("recordIds", new java.util.ArrayList<String>());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 根据record_id删除IoTDB中的文件记录（支持GET和DELETE方法）
     * 
     * @param recordId 记录唯一标识（UUID）
     * @return 删除结果
     */
    @GetMapping("/delete-by-record-id")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> deleteByRecordId(@RequestParam("recordId") String recordId) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            log.info("收到删除请求 - recordId: {}", recordId);
            
            // 参数验证
            if (recordId == null || recordId.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "record_id参数不能为空");
                return ResponseEntity.badRequest().body(result);
            }
            
            // 调用Service层方法执行删除
            Map<String, Object> deleteResult = iotdbService.deleteFileByRecordId(recordId);
            
            // 返回删除结果
            if ((Boolean) deleteResult.get("success")) {
                result.put("success", true);
                result.put("message", deleteResult.get("message"));
                result.put("recordId", deleteResult.get("recordId"));
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", deleteResult.get("message"));
                return ResponseEntity.badRequest().body(result);
            }
            
        } catch (Exception e) {
            log.error("删除IoTDB记录时发生异常 - recordId: {}", recordId, e);
            result.put("success", false);
            result.put("message", "删除失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}

